import sys
import getopt


# 使用函数 getopt.getopt, 以及出错的getout.GetoptError
# 使用命令行输入参数使用函数

# 短选项模式 网站参数
def siteOne():
    name = None
    url = None

    argv = sys.argv[1:]

    try:
        opts, args = getopt.getopt(argv, "n:u:") # 短选项模式
    except:
        print("Error")

    for opt, arg in opts:
        if opt in ['-n']:
            name = arg
        elif opt in ['-u']:
            url = arg

    print(name + " " + url)


# 长选项模式 网站参数
def siteTwo():
    name = None
    url = None

    argv = sys.argv[1:]

    try:
        opts, args = getopt.getopt(argv, "n:u:", ["name=", "url="])  # 长选项模式
    except:
        print("Error")

    for opt, arg in opts:
        if opt in ['-n', '--name']:
            name = arg
        elif opt in ['-u', '--url']:
            url = arg

    print(name + " " + url)


# 文件参数
def main(argv):
    inputfile = ''
    outputfile = ''

    try:
        opts, args = getopt.getopt(argv, "hi:o:", ["ifile=", "ofile"])

    except getopt.GetoptError:
        print("command_line_arguments.py error")
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            print("command_line_arguments.py -h")
            sys.exit()
        elif opt in ("-i", "--ifile"):
            inputfile = arg
        elif opt in ("-o", "--ofile"):
            outputfile = arg

    print("输入文件为: ", inputfile)
    print("输出文件为: ", outputfile)

if __name__ == "__main__":
    main(sys.argv[1:])